#pragma once
#include <string>
#include <vector>
#include <chrono>
#include <functional>

struct DailyData {
    std::string date;        // Format: "YYYY-MM-DD"
    int clickCount;
    
    DailyData(const std::string& d = "", int count = 0) 
        : date(d), clickCount(count) {}
};

class DataManager {
public:
    DataManager();
    ~DataManager();
    
    // Initialize data manager and load today's data
    bool Initialize();
    
    // Record a single key press
    void OnKeyPressed();
    
    // Get current click count
    int GetCurrentClickCount() const { return m_currentClickCount; }
    
    // Manually save data
    void SaveData();
    
    // Reset today's click count to 0
    void ResetTodayCount();
    
    // Get history data (for statistics etc)
    const std::vector<DailyData>& GetHistoryData() const { return m_historyData; }
    
private:
    // Save trigger conditions
    static const int SAVE_INTERVAL_CLICKS = 100;           // Save every 100 clicks
    static const int SAVE_INTERVAL_SECONDS = 300;          // Save every 5 minutes
    static const int MAX_HISTORY_DAYS = 30;                // Keep 30 days of history
    static const int MAX_CLICK_COUNT = 9999999;            // Maximum click count before reset
    
    // Current data
    int m_currentClickCount;
    int m_unsavedClicks;
    std::string m_currentDate;
    std::chrono::steady_clock::time_point m_lastSaveTime;
    
    // History data
    std::vector<DailyData> m_historyData;
    
    // Data file path
    std::string m_dataFilePath;
    
    // Internal methods
    std::string GetCurrentDateString() const;
    std::string GetDataFilePath() const;
    bool LoadDataFromFile();
    bool SaveDataToFile();
    void CleanupOldData();
    void CheckDateChange();
    bool ShouldSave() const;
    
    // JSON processing methods
    std::string SerializeToJson() const;
    bool DeserializeFromJson(const std::string& jsonContent);
    
    // File operation helper methods
    bool CreateDirectoryIfNotExists(const std::string& path) const;
    bool FileExists(const std::string& path) const;
    std::string ReadFileContent(const std::string& path) const;
    bool WriteFileContent(const std::string& path, const std::string& content) const;
};
